home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Toolbox / MenuHook⁄MBarHook exposed / Hooks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-24  |  5.9 KB  |  332 lines  |  [TEXT/KAHL]

  1. /**------------------------------------------------------------------------------------- 
  2. *
  3. *
  4. *  MenuHook/MBarHook exposed
  5. *  Visual proof these things do get called!
  6. *
  7. *
  8. *  Matt Deatherage   12/07/92
  9. *
  10. *
  11.  **-----------------------------------------------------------------------------------**/
  12.  
  13. #define rMenuBar 128
  14. #define mApple 128
  15. #define mFile 129
  16. #define mChoices 133
  17. #define rAboutAlert 128
  18. #define iAbout 1
  19. #define iQuit 8
  20. #define rWindow 128
  21. #define SetMBarHook ((ProcPtr *)MBarHook)        // necessary for MPW
  22. #define SetMenuHook ((ProcPtr *)MenuHook)        // necessary for MPW
  23.  
  24.  
  25. void InitMyStuff(void);
  26. void DoEventLoop(void);
  27. void DoUpdate(WindowPtr window);
  28. pascal void MyMBarHook(Rect * theRect);
  29. pascal void MyMenuHook(void);
  30.  
  31. /*------ main ----------------------------------------------------------------------------*/
  32.  
  33. Rect Rect1,Rect2;
  34. WindowPtr myWindow;
  35. Boolean gQuit;
  36.  
  37. main()
  38.  
  39. {
  40.  
  41. GrafPort myPort;
  42.  
  43.     InitGraf((Ptr) &thePort);
  44.     OpenPort(&myPort);
  45.     InitFonts();
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(nil);
  50.     InitCursor();
  51.  
  52.     SetRect(&Rect1,5,5,15,15);
  53.     SetRect(&Rect2,5,20,15,30);
  54.     
  55.     InitMyStuff();
  56.     DoEventLoop();
  57.     CloseWindow(myWindow);
  58.     
  59. } /* main */
  60.  
  61. /*------ InitMyStuff ---------------------------------------------------------------------*/
  62.  
  63. void InitMyStuff()
  64.  
  65. {
  66.  
  67. Handle menuBar;
  68. MenuHandle theMenu;
  69.  
  70.  
  71.     menuBar = GetNewMBar(rMenuBar);
  72.     if (menuBar == NULL)
  73.         ExitToShell();
  74.     SetMenuBar(menuBar);
  75.     DisposHandle(menuBar);
  76.     
  77.     theMenu = GetMenu(mChoices);
  78.     InsertMenu(theMenu, -1);                    /* stick this in the hierarchical list */
  79.     
  80.     AddResMenu(GetMHandle(mApple),'DRVR');        /* DA names to the Apple menu */
  81.     DrawMenuBar();
  82.     
  83.     // *SetMBarHook = MyMBarHook;  for MPW
  84.     MBarHook = MyMBarHook;                        // for THINK C
  85.     
  86.     // *SetMenuHook = MyMenuHook;  for MPW
  87.     MenuHook = MyMenuHook;
  88.  
  89.     myWindow = GetNewWindow(rWindow, NULL, (WindowPtr)-1L);
  90.     ShowWindow(myWindow);    
  91.     SetPort(myWindow);
  92.     
  93.     
  94.     
  95.     
  96. }
  97.  
  98.  
  99. /*------ DoMenuCommand -------------------------------------------------------------------*/
  100.  
  101.  
  102. void DoMenuCommand(menuResult)
  103. long menuResult;
  104.  
  105. {
  106.   short menuID; /*the ID of the selected menu*/
  107.   short menuItem; /*the item number of the selected menu*/
  108.   short itemHit;
  109.   Str255 daName;
  110.   short daRefNum;
  111.  
  112.  
  113.     menuID = (menuResult >> 16);         /*use built-ins (for efficiency)...*/
  114.     menuItem = (menuResult & 0xFFFF);         /*to get menu item number and menu number*/
  115.     switch (menuID) {
  116.         case mApple:  
  117.                 switch (menuItem){ 
  118.  
  119.                      case iAbout: 
  120.                       itemHit = Alert(rAboutAlert, nil);
  121.                       break;
  122.                   
  123.                  default:
  124.                        /*all non-About items in this menu are DAs*/
  125.                      GetItem(GetMHandle(mApple),menuItem,daName);
  126.                       daRefNum = OpenDeskAcc(daName);
  127.                      break;
  128.             }
  129.         
  130.         case mFile: 
  131.             switch (menuItem){ 
  132.                  case iQuit: 
  133.                       gQuit = TRUE;
  134.            
  135.            default:
  136.            break;
  137.            }
  138.     };
  139.     HiliteMenu(0); /*unhighlight what MenuSelect (or MenuKey) hilited*/
  140.  }
  141.  
  142.  
  143. /*------ DoEvent ------------------------------------------------------------------------*/
  144.  
  145.  
  146. void DoEvent(event)
  147.  
  148. EventRecord * event;
  149.  
  150. {
  151. short part, err;
  152. WindowPtr window;
  153. Point aPoint;
  154. char key;
  155.  
  156.      switch (event->what) {
  157.         case mouseDown:
  158.         {
  159.             part = FindWindow(event->where, &window);
  160.             switch (part) {
  161.                 case inMenuBar:            /*process the menu command*/
  162.                         { 
  163.                             DoMenuCommand(MenuSelect(event->where));
  164.                             break;
  165.                         };
  166.                 case inSysWindow:    /*let the system handle the mouseDown*/
  167.                         { 
  168.                         SystemClick(event, window);
  169.                         break;
  170.                         }
  171.                 case inDrag:        /*pass screenBits.bounds to get all gDevices*/
  172.                         {
  173.                         DragWindow(window, event->where, &screenBits.bounds);
  174.                         break;
  175.                         }
  176.                 default:
  177.                     ;
  178.             };
  179.             break;
  180.         };
  181.         case keyDown:
  182.         case autoKey:
  183.         { /*check for menukey equivalents*/
  184.             key = (event->message & charCodeMask);
  185.             if (event->modifiers & cmdKey) /*Command key down*/
  186.                 if ( event->what == keyDown)
  187.                 {
  188.                     DoMenuCommand(MenuKey(key));
  189.                 };
  190.             break;
  191.         }; 
  192.         case updateEvt:
  193.             {
  194.             DoUpdate((WindowPtr)event->message);
  195.             break;
  196.             }
  197.         case diskEvt:
  198.             {
  199.                 if ( (event->message) != noErr )
  200.                 {
  201.                     SetPt(&aPoint,50,50);
  202.                     err = DIBadMount(aPoint, event->message);
  203.                 };
  204.                 break;
  205.             }
  206.         default:;
  207.     };
  208. }
  209. /*DoEvent*/
  210.  
  211. /*------ DoUpdate ----------------------------------------------------------------------*/
  212.  
  213. void DoUpdate(window)
  214. WindowPtr window;
  215. {
  216.  
  217.     Rect tempRect;
  218.  
  219.     SetPort(window);
  220.     BeginUpdate(window);             /*sets up the visRgn, clears updateRgn*/
  221.     if (!EmptyRgn(window->visRgn))     /*draw if updating needs to be done*/
  222.     {
  223. /*        tempRect = (*(window->visRgn))->rgnBBox */
  224.         tempRect = (*(window->visRgn))->rgnBBox;
  225.         EraseRect(&tempRect);
  226.         FrameRect(&Rect1);
  227.         FrameRect(&Rect2);
  228.         MoveTo(20,15);
  229.         DrawString("\pMenuHook being called");
  230.         MoveTo(20,30);
  231.         DrawString("\pMBarHook being called");
  232.         EndUpdate(window); /*restores the visRgn*/
  233.     }
  234. }
  235.  
  236. /*------ DoEventLoop -----------------------------------------------------------------------*/
  237.  
  238. void DoEventLoop()
  239.  
  240. {
  241.  
  242. Boolean gotEvent;
  243. EventRecord event;
  244.  
  245.  
  246.     gQuit = FALSE;
  247.     
  248.     do {
  249.         /*put us 'asleep' forever under MultiFinder*/
  250.         gotEvent = WaitNextEvent(everyEvent, &event, 0xFFFFFFFF, NULL);
  251.  
  252.         if ( gotEvent )
  253.             DoEvent(&event);
  254.     }
  255.     while (gQuit == FALSE); /*loop forever; we quit through an ExitToShell*/
  256. }
  257.  
  258.  
  259. /*------ MyMBarHook -----------------------------------------------------------------------*/
  260.  
  261. pascal void MyMBarHook(theRect)
  262.  
  263. Rect * theRect;
  264.  
  265. {
  266.  
  267. GrafPtr oldPort;
  268. long    theTicks;
  269.  
  270.     GetPort(&oldPort);
  271.     SetPort(myWindow);
  272.     
  273.     theTicks = TickCount();
  274.     InvertRect(&Rect2);
  275.     while (theTicks + 10  >= TickCount());
  276.     InvertRect(&Rect2);
  277.     
  278.     SetPort(oldPort);
  279.  
  280.  
  281.     InvertRect(theRect);
  282.     
  283.     
  284.     asm {
  285.         moveq.l #0,D0
  286.     };
  287.     
  288. }
  289.  
  290. /*------ MyMenuHook -----------------------------------------------------------------------*/
  291.  
  292. pascal void MyMenuHook()
  293.  
  294. {
  295.  
  296. GrafPtr oldPort;
  297. long     theTicks;
  298.  
  299.     GetPort(&oldPort);
  300.     SetPort(myWindow);
  301.     
  302.     theTicks = TickCount();
  303.     InvertRect(&Rect1);
  304.     while (theTicks + 1 >= TickCount());
  305.     InvertRect(&Rect1);
  306.     while (theTicks + 1 >= TickCount());
  307.     
  308.     SetPort(oldPort);
  309.     
  310. }
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.